package com.example.covid19.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.covid19.common.Dto.ChanbedDto;
import com.example.covid19.common.Dto.Editdto;
import com.example.covid19.common.lang.Result;
import com.example.covid19.entity.Chanbed;
import com.example.covid19.entity.StationMessage;
import com.example.covid19.entity.Student;
import com.example.covid19.entity.Stustatus;
import com.example.covid19.mapper.ChanbedMapper;
import com.example.covid19.service.ChanbedService;
import com.example.covid19.service.StationMessageService;
import com.example.covid19.service.StudentService;
import com.example.covid19.service.StustatusService;
import com.example.covid19.util.MessageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author BaiZhengChun
 * @since 2023-04-03
 */
@RestController
@RequestMapping("/chanbed")
public class ChanbedController {
    @Autowired
    ChanbedService chanbedService;
    @Autowired
    ChanbedMapper chanbedMapper;
    @Autowired
    StudentService studentService;
    @Autowired
    StationMessageService stationMessageService;
    @Autowired
    StustatusService stustatusService;
    @Autowired
    MessageUtil messageUtil;

    /**
     *
     * @param httpServletRequest
     * @return 查看对应的换寝审批请求
     */
    @RequestMapping("/review")
    public Result review(HttpServletRequest httpServletRequest){
        String s = (String) StpUtil.getLoginIdByToken(httpServletRequest.getHeader("satoken"));
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<ChanbedDto>chanbedDtoList = chanbedMapper.getChanbedInfo(Integer.valueOf(s));
        return Result.success(200,"成功", chanbedDtoList);
    }

    /**
     *
     * @param editdto
     * @return 同意该请求 并发送给对应的学生
     */
    @RequestMapping("/suc")
    public Result suc(@RequestBody Editdto editdto){
        Chanbed chanbed = chanbedService.getOne(new QueryWrapper<Chanbed>().eq("id",editdto.getBiaoshi()));
        chanbed.setProcess("1");
        chanbed.setIson(1);
        chanbedService.updateById(chanbed);
        messageUtil.sendAny(1,chanbed.getRid(), String.valueOf(chanbed.getId()));
        return Result.success(200,"成功",null);
    }

    /**
     *
     * @param editdto
     * @return拒绝该请求
     */
    @RequestMapping("/nopass")
    public Result nopass(@RequestBody Editdto editdto){
        Chanbed chanbed = chanbedService.getOne(new QueryWrapper<Chanbed>().eq("id",editdto.getBiaoshi()));
        chanbed.setProcess("2");
        chanbed.setIson(1);
        chanbedService.updateById(chanbed);
        messageUtil.sendone(String.valueOf(chanbed.getStudentid()),6);
        return Result.success(200,"成功",null);
    }

    /**
     * 学生审批换寝室申请人得换寝请求,根据state判断
     */
    @RequestMapping("/ssbed")
    public Result ssbed(@RequestBody Editdto editdto){
        StationMessage stationMessage = stationMessageService.getOne(new QueryWrapper<StationMessage>().eq("id",editdto.getUserid()));
        int i = stationMessageService.list(new QueryWrapper<StationMessage>().eq("uid",stationMessage.getUid())).size();
        if (editdto.getBiaoshi()==0){
            //同意
            stationMessage.setResult(1);
            stationMessageService.updateById(stationMessage);
            //都同意后就发送请求成功
        if (stationMessageService.list(new QueryWrapper<StationMessage>().eq("uid",stationMessage.getUid()).eq("result",1)).size()==i){
            messageUtil.sendone(String.valueOf(stustatusService.getOne(new QueryWrapper<Stustatus>().eq("bbid",stationMessage.getUid())).getStudentid()),5);
        }
        }else {
            //拒绝
            stationMessage.setResult(2);
            List<StationMessage>stationMessageList = stationMessageService.list(new QueryWrapper<StationMessage>().eq("uid",stationMessage.getUid()).ne("studentid",stationMessage.getStudentid()));
            stationMessageList.forEach(v->{
                v.setResult(3);
            });
            stationMessageService.saveOrUpdateBatch(stationMessageList);
            //给那个用户发送拒绝的信息
            messageUtil.sendone(String.valueOf(stustatusService.getOne(new QueryWrapper<Stustatus>().eq("bbid",editdto.getUserid())).getStudentid()),6);
        }
        return Result.success(200,"ok",null);
    }
    /**
     * 学生如果7内不回复就认为是同意
     */
    @RequestMapping("userAck")
    public void userAck(){
        //取出今天要自动同意的列表
        List<StationMessage>stationMessageList = stationMessageService.list(new QueryWrapper<StationMessage>().eq("template",1).eq("expiration_time", LocalDate.now().minusDays(1)));
//        根据uid来自动同意（仅没答复）
        stationMessageList.stream().map(StationMessage::getUid).distinct().collect(Collectors.toList()).forEach(v->{
            int i = stationMessageService.list(new QueryWrapper<StationMessage>().eq("uid",v)).size();
            stationMessageService.list(new QueryWrapper<StationMessage>().eq("uid",v)).forEach(k->{
                if (k.getResult()==0){
                    k.setResult(1);
                    stationMessageService.updateById(k);
                }
            });
            //如果自动答复后都同意了那么发送站内信，因为一个拒绝全部认定为拒绝所以只需要发送同意情况的就可
            if (stationMessageService.list(new QueryWrapper<StationMessage>().eq("uid",v).eq("result",1)).size()==i){
                messageUtil.sendone(String.valueOf(stustatusService.getOne(new QueryWrapper<Stustatus>().eq("bbid",v)).getStudentid()),5);
            }
        });

    }
}
